package com.course.business.controller.admin;

import com.course.server.dto.ChapterDto;
import com.course.server.dto.ChapterPageDto;
import com.course.server.dto.PageDto;
import com.course.server.dto.ResponseDto;
import com.course.server.service.ChapterService;
import com.course.server.utils.ValidatorUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 〈一句话功能简述>
 * 〈〉
 *
 * @Author metype
 * @Create 2021/5/25
 * @Since 1.0.0
 */
@RestController
@RequestMapping("/admin/chapter")
@Api(tags = "大章管理")
public class ChapterController {

    private static final Logger LOG = LoggerFactory.getLogger(ChapterController.class);
    public static final String BUSINESS_NAME = "大章";

    @Autowired
    private ChapterService chapterService;

    @RequestMapping("/list")
    @ApiOperation(value="发送解析文本", notes="发送解析文本", produces="application/json")
    public ResponseDto <PageDto> test(@RequestBody ChapterPageDto chapterPageDto) {
        LOG.info("pageDto:{}", chapterPageDto);
        ValidatorUtil.require(chapterPageDto.getCourseId(), "课程id");
        chapterService.list(chapterPageDto);
        return new ResponseDto <>(chapterPageDto);
    }

    @RequestMapping("/save")
    public ResponseDto <ChapterDto> save(@RequestBody ChapterDto chapterDto) {
        LOG.info("chapterDto:{}", chapterDto);
        // 保存校验
        ValidatorUtil.require(chapterDto.getName(), "名称");
        ValidatorUtil.require(chapterDto.getCourseId(), "名称");
        ValidatorUtil.length(chapterDto.getCourseId(), "名称", 1, 8);

        chapterService.save(chapterDto);
        return new ResponseDto <>(chapterDto);
    }

    @RequestMapping("/del/{id}")
    public ResponseDto del(@PathVariable String id) {
        LOG.info("id:{}", id);
        chapterService.del(id);
        return new ResponseDto <>("200", "删除成功！");
    }

}